﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace InspectionVehicule.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:MultipleEditGridView runat=server></{0}:MultipleEditGridView>")]
    public class MultipleEditGridView : System.Web.UI.WebControls.GridView
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            } 
            set
            {
                ViewState["Text"] = value;
            }
        } 

        /// <summary>
        /// Default Constructor
        /// </summary>
        public MultipleEditGridView()
        {
            if(this.ViewState["DisplayMode"] == null) this.ViewState["DisplayMode"] = 0;
        } 

        /// <summary>
        /// Emulates the creation of the row to set all rows as parent.
        /// </summary>
        /// <param name=”rowIndex”></param>
        /// <param name=”dataSourceIndex”></param>
        /// <param name=”rowType”></param>
        /// <param name=”rowState”></param>
        /// <returns></returns>
        protected override GridViewRow CreateRow(int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
        {
            int displayMode = 0;

            if (this.ViewState["DisplayMode"] != null)
                 int.TryParse(this.ViewState["DisplayMode"].ToString(), out displayMode); 

            return base.CreateRow(rowIndex, dataSourceIndex, rowType, (displayMode == 1 ? DataControlRowState.Edit : rowState));
        } 

        public int DisplayMode
        {
          get { return (int)this.ViewState["DisplayMode"]; }
          set { this.ViewState["DisplayMode"] = value; }
        }
    }
}